package com.zheng.oa.admin.controller.wc;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zheng.common.base.BaseController;
import com.zheng.common.util.CamelUnderlineConverter;
import com.zheng.common.util.Pagination;
import com.zheng.common.util.PoiUtil;
import com.zheng.hr.dao.model.HrDict;
import com.zheng.hr.rpc.api.HrDictService;
import com.zheng.oa.common.constant.OaResult;
import com.zheng.oa.common.constant.OaResultConstant;
import com.zheng.oa.common.constant.enums.OaStatus;
import com.zheng.oa.common.constant.enums.PlanTimeType;
import com.zheng.oa.common.constant.enums.PlanType;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.upms.rpc.api.UpmsOrganizationService;
import com.zheng.wc.dao.model.WcNotice;
import com.zheng.wc.dao.model.WcPlan;
import com.zheng.wc.rpc.api.WcNoticeService;
import com.zheng.wc.rpc.api.WcPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/3/30
 * Time: 9:28
 */
@Controller
@Api(value = "通知管理", description = "通知管理")
@RequestMapping("/oa/wc/notice")
public class WcNoticeController extends BaseController {
    @Autowired
    private WcNoticeService noticeService;
    @Autowired
    private UpmsApiService apiService;
    @Autowired
    private HrDictService dictService;

    @ApiOperation(value = "通知管理")
    @RequiresPermissions("oa:wc:notice:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return "/manage/wc/notice/index.jsp";
    }

    @ApiOperation(value = "通知管理列表")
    @RequiresPermissions("oa:wc:notice:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "1", value = "pageNumber") int pageNumber,
                       @RequestParam(required = false, defaultValue = "10", value = "pageSize") int pageSize,
                       @RequestParam(required = false, value = "sortName") String sortName,
                       @RequestParam(required = false, value = "sortOrder") String sortOrder,
                       WcNotice notice) {
        notice.getSqlMap().put("sql", apiService.sqlDataScopeFilter(getUsername(), "wn", "uu"));
        notice.setPagination(new Pagination(pageNumber, pageSize, sortOrder, sortName));
        notice.setStatus(OaStatus.NORMAL.getKey());
        List<Map<String, Object>> rows = noticeService.selectNoticePage(notice);
        Integer total = noticeService.selectNoticePageCount(notice);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增通知管理")
    @RequiresPermissions("oa:wc:notice:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        return "/manage/wc/notice/create.jsp";
    }

    @ApiOperation(value = "新增通知管理")
    @RequiresPermissions("oa:wc:notice:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody JSONObject jsonObject) throws Exception {
        UpmsUser upmsUser = apiService.selectUpmsUserByUsername(getUsername());

        WcNotice notice = jsonObject.toJavaObject(WcNotice.class);
        notice.setCuser(upmsUser.getUserId());
        notice.setCtime(new Date());
        notice.setCorganization(upmsUser.getOrganizationId());
        notice.setStatus(OaStatus.NORMAL.getKey());
        String dictValue = dictService.getDictValue("草稿", "notice_state", "0");
        if (StringUtils.isNotBlank(dictValue)) {
            notice.setState(Integer.parseInt(dictValue));
        }
        JSONArray noticeUserIds = jsonObject.getJSONArray("noticeUserIds");
        List<Integer> userIds = noticeUserIds.toJavaList(Integer.class);

        try {
            noticeService.insertNotice(notice, userIds, null);
        } catch (Exception e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }

    @ApiOperation(value = "修改通知管理状态")
    @RequiresPermissions("oa:wc:notice:update")
    @RequestMapping(value = "/updateState", method = RequestMethod.GET)
    @ResponseBody
    public Object updateState(String ids, Integer state) {
        if (noticeService.updateState(ids, state)) {
            return new OaResult(OaResultConstant.SUCCESS, null);
        }
        return new OaResult(OaResultConstant.FAILED, null);
    }

    @ApiOperation(value = "删除通知管理")
    @RequiresPermissions("oa:wc:notice:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        try {
            noticeService.deletePlans(ids);
        } catch (Exception e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }

}
